<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:fo="http://www.w3.org/1999/XSL/Format"
	xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
			 xmlns:s="http://www.w3.org/2001/XMLSchema"
			 xmlns:s0="http://tempuri.org/" 
			 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
			 xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"
			 xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/">
  <xsl:variable name="service.name" select="//*[local-name()='service']/@name"/>
	<xsl:variable name="service.endpoint" select="//*[local-name()='service']/*[local-name()='port']/*[local-name()='address']/@location"/>
	<xsl:variable name="service.namespace" select="//*[local-name()='schema']/@targetNamespace"/>

	<xsl:output method="text"/>
	
<xsl:template match="/">&lt;?
define("ENDPOINT", "<xsl:value-of select="$service.endpoint"/>");
define("NAMESPACE", "<xsl:value-of select="$service.namespace"/>");
class <xsl:value-of select="$service.name"/>
{
    function <xsl:value-of select="$service.name"/>()
    {
        //
        // TODO: Add any constructor code required
        //
    }
    
<xsl:apply-templates select="//*[local-name()='portType']"/>
    
    function envelope ($data)
    {
        $output='';
        $output.='&lt;soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;';                            
        $output.='&lt;soap:Body&gt;' . $data;  
        $output.='&lt;/soap:Body&gt;';  
        $output.='&lt;/soap:Envelope&gt;';  
        return $output;
    }

    function invoke ($data, $nodename, $action, $array, $proxy=NULL) // invoke                                                                                                                                                                                                                                                                               
    {
        $endpoint=ENDPOINT;
        for ($e=0;$e&lt;2;$e++) $endpoint = substr($endpoint,strpos ($endpoint,'/')+1);                                                                                                                                                                                                      
        $domain = substr($endpoint, 0, strpos($endpoint,'/'));                                                                                                                                                                                                   
        $address = substr($endpoint, strpos($endpoint,'/'));     
        $response=$this->connect($this->envelope($data), $domain, $address, $action); 
        $result=$this->deserialize ($response,$nodename,$proxy);
        return $array ? $result : $result[0];	
    } // invoke   


    function deserialize($data,$nodename,$proxy=NULL)
    {
        $result=array();
        if ($dom = domxml_open_mem( $data ) ) 
        {
            foreach ($dom->get_elements_by_tagname($nodename) as $node)
            {
                if ($proxy)
                {
                    eval ('$tmp = new ' . $proxy . '();');
                    $tmp->deserialize ($node);
                    $result[]=$tmp;
                }
                else
                {
                    $result[]=$node-&gt;get_content(); 		
                }
            }
        } else echo ("The response was not well-formed XML." . $data);
        return $result;
    }     

    function sendToPHPDc ($host,$method,$path,$data,$useragent=NULL,$action=NULL) {                                                                                                                                                                                               
        if (empty($method)) {                                                                                                                                                                                               
            $method = 'GET';                                                                                                                                                                                               
        }                  
		$port=80; 
		if (strpos($host,':')!==false)
		{
			$port=explode (':', $host);
			$host=$port[0];
			$port=$port[1];
		}
		                                                                                                                                                                            
        $method = strtoupper($method);                                                                                                                                                                                               
        $fp = fsockopen($host,$port);                                                                                                                                                                                               
        if ($fp) {                                                                                                                                                                                               
            if ($method == 'GET') {                                                                                                                                                                                               
                $path .= '?' . $data;                                                                                                                                                                                               
            }                                                                                                                                                                                               
            fputs($fp, "$method $path HTTP/1.0\r\n");                                                                                                                                                                                               
            fputs($fp, "Host: $host\r\n");                                                                                                                                                                                               
            fputs($fp, "Content-type: text/xml\r\n");                                                                                                                                                                                               
            fputs($fp, "Content-length: " . strlen($data) . "\r\n");                                                                                                                                                                                               
            if ($useragent)                                                                                                                                                                                               
                fputs($fp, "User-Agent: MSIE\r\n");                                                                                                                                                                                               
            if ($action)                                                                                                                                                                                               
                fputs($fp, "SOAPAction: " . $action . "\r\n");                                                                                                                                                                                               
            fputs($fp, "Connection: close\r\n\r\n");                                                                                                                                                                                               
            if ($method == 'POST') {                                                                                                                                                                                               
                fputs($fp, $data);                                                                                                                                                                                               
            }                                                                                                                                                                                               
            while (!feof($fp))                                                                                                                                                                                               
                $buf .= fgets($fp,1024);                                                                                                                                                                                               
            fclose($fp);                                                                                                                                                                                               
        }                                                                                                                                                                                               
        $p = strpos ( $buf, "&lt;");                                                                                                                                                                                               
        $buf = substr($buf, $p);                                                                                                                                                                                               
        return $buf;                                                                                                                                                                                               
    }                                                                                                                                                                                               
                                                                                                                                                                                                
    function connect($data, $domain, $url, $action)                                                                                                                                                          
    {                                                                                                                                                                                               
        return $this->sendToPHPDc ($domain, "POST", $url, $data, 0,$action);                                                                                                                                                                                                
    } 
}

<xsl:apply-templates select="//*[local-name()='schema']/*[local-name()='complexType']"/>
?&gt;
</xsl:template>


	<xsl:template match="//*[local-name()='element']">
	<xsl:for-each select="./*[local-name()='complexType']/*[local-name()='sequence']/*[local-name()='element']"
		>$<xsl:value-of select="@name"/> <xsl:if test="following-sibling::*[local-name()='element']">, </xsl:if>
	</xsl:for-each>
</xsl:template>


	<xsl:template match="//*[local-name()='operation']">
		<xsl:variable name="name" select="@name"/>
    <xsl:variable name="response" select="//*[local-name()='element'][@name=concat($name,'Response')]"/>
    <xsl:variable name="soapaction" select="//*[local-name()='operation'][@name=$name]/*[local-name()='operation'][@soapAction]/@soapAction"/>
		<xsl:variable name="type" select="$response/*[local-name()='complexType']/*[local-name()='sequence']/*[local-name()='element']"/>
		<xsl:variable name="type.name">
			<xsl:choose>
				<xsl:when test="contains($type/@type,':')">
					<xsl:value-of  select="substring-after($type/@type,':')"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$type/@type"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="type.max">
			<xsl:choose>
				<xsl:when test="$type/@maxOccurs='unbounded'">true</xsl:when>
				<xsl:otherwise>false</xsl:otherwise>
			</xsl:choose>
		</xsl:variable> 
    <xsl:variable name="type.proxy">
      <xsl:choose>
        <xsl:when test="substring-before($type/@type,':')='s'">NULL</xsl:when>
        <xsl:otherwise>"<xsl:value-of select="$type.name"/>"</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
<xsl:if test="not (preceding::*[local-name()='operation'][@name=$name])">
    function <xsl:value-of select="@name"/> (<xsl:apply-templates select="//*[local-name()='element'][@name=$name]"/>)
    {
        $is_array=<xsl:value-of select="$type.max"/>;
        $request="&lt;<xsl:value-of select="@name"/> xmlns=\"" . NAMESPACE . "\"&gt;";<xsl:call-template name="form.invoke">
<xsl:with-param name="method" select="@name"/>
</xsl:call-template>
        $request.="&lt;/<xsl:value-of select="@name"/>&gt;";
        return $this->invoke ($request, "<xsl:value-of select="@name"/>Result", "<xsl:value-of select="$soapaction"/>", $is_array, <xsl:value-of select="$type.proxy"/>);
    }

</xsl:if>
		
	</xsl:template>

	
<xsl:template name="form.invoke">
	<xsl:param name="method"/>
	<xsl:param name="for" select="//*[local-name()='element'][@name=$method]/*[local-name()='complexType']/*[local-name()='sequence']/*[local-name()='element']"/>
	<xsl:param name="me" select="$for[1]"/>
	<xsl:param name="next" select="$for[position()!=1]"/><xsl:if test="$me"> 
        $request.="&lt;<xsl:value-of select="$me/@name"/>&gt;" . $<xsl:value-of select="$me/@name"/> . "&lt;/<xsl:value-of select="$me/@name"/>&gt;";<xsl:if test="$next">
<xsl:call-template name="form.invoke">
<xsl:with-param name="for" select="$next"/>
</xsl:call-template>
	</xsl:if></xsl:if>
</xsl:template>

	<xsl:template match="//*[local-name()='portType']">

		<xsl:apply-templates select="./*[local-name()='operation']">
		</xsl:apply-templates>
	</xsl:template>


  <xsl:template match="//*[local-name()='schema']/*[local-name()='complexType']"> 
Class <xsl:value-of select="@name"/>  
{<xsl:for-each select="./*/*[local-name()='element']">
    var $<xsl:value-of select="@name"/><xsl:if test="@maxOccurs='unbounded'"> = array()</xsl:if>;</xsl:for-each>
    function deserialize($element)
    {
<xsl:for-each select="./*/*[local-name()='element']"><xsl:choose>
  <xsl:when test="@maxOccurs='unbounded'">
        <xsl:value-of select="concat('        $',@name)"/>s=$element-&gt;get_elements_by_tagname("<xsl:value-of select="@name"/>");   
        for ($i=0;$i&lt;count($<xsl:value-of select="@name"/>s);$i++)
        {<xsl:choose>
          <xsl:when test="substring-before(@type,':')='s'">
            $this-&gt;<xsl:value-of select="@name"/>[]=$<xsl:value-of select="@name"/>s[$i]-&gt;get_content;</xsl:when> 
          <xsl:otherwise>
            $o<xsl:value-of select="@name"/>=new <xsl:value-of select="substring-after(@type,':')"/>(); 
            $o<xsl:value-of select="@name"/>-&gt;deserialize ($<xsl:value-of select="@name"/>s[$i]);  
            $this-&gt;<xsl:value-of select="@name"/>[]=$o<xsl:value-of select="@name"/>;</xsl:otherwise> 
        </xsl:choose> 
        }  </xsl:when> 
  <xsl:otherwise><xsl:choose><xsl:when test="substring-before(@type,':')='s'">
<xsl:value-of select="concat('        $node',@name)"/>=$element-&gt;get_elements_by_tagname("<xsl:value-of select="@name"/>");   
        if (count(<xsl:value-of select="concat('$node',@name)"/>)&gt;0)
<xsl:value-of select="concat('            $this-&gt;',@name)"/>=<xsl:value-of select="concat('$node',@name)"/>[0]-&gt;get_content();
</xsl:when> 
          <xsl:otherwise>
        $o<xsl:value-of select="@name"/>=new <xsl:value-of select="substring-after(@type,':')"/>(); 
        $o<xsl:value-of select="@name"/>-&gt;deserialize ($element);
        $this-&gt;<xsl:value-of select="@name"/>=$o<xsl:value-of select="@name"/>;</xsl:otherwise> 
        </xsl:choose></xsl:otherwise>
</xsl:choose></xsl:for-each> 
    }
}

</xsl:template>


</xsl:stylesheet> 
